/**
 * created by 小卷毛, 2020/10/30
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.lib_tools_database.factory;

import android.text.TextUtils;

import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureSave;
import com.sgcc.pda.lib_tools_database.bean.ticket.LocalImage;
import com.sgcc.pda.lib_tools_database.gen.DaoSession;
import com.sgcc.pda.lib_tools_database.gen.LocalImageDao;
import com.sgcc.pda.lib_tools_database.gen.TicketMeasureSaveDao;

import org.greenrobot.greendao.Property;

import java.util.List;

/**
 * 描 述：工作票防范措施表操作类
 * 作 者：小卷毛  2020-10-30 15:22
 * 修改描述：增加保存附件模式
 * 修 改 人：
 * 修改版本：v1.2.1
 */
public class TicketMeasureSaveImpl extends Abstractable<TicketMeasureSave, TicketMeasureSaveDao> {

    @Override
    public TicketMeasureSaveDao getDao(DaoSession session) {
        return session.getTicketMeasureSaveDao();
    }

    /**
     * 删除多条记录
     */
    public void delete(List<TicketMeasureSave> list) {
        mCommonDaoUtils.delete(list);
    }

    @Override
    public void insertList(List<TicketMeasureSave> list) {
        super.insertList(list);

        for (TicketMeasureSave item : list) {
            // 查询在线模式的本地附件
            List<LocalImage> childOnlineList = new LocalImageImpl().findByParam(
                    new Property[]{
                            LocalImageDao.Properties.ParentId,
                            LocalImageDao.Properties.AttachMode
                    },
                    new String[]{item.getRecordId(), "5"}
            );

            if (childOnlineList != null) {
                new LocalImageImpl().delete(childOnlineList);
            }

            // 查询离线模式的本地附件
            List<LocalImage> childOfflineList = new LocalImageImpl().findByParam(
                    new Property[]{
                            LocalImageDao.Properties.ParentId,
                            LocalImageDao.Properties.AttachMode
                    },
                    new String[]{item.getRecordId(), "3"}
            );

            if (childOfflineList != null) {
                new LocalImageImpl().delete(childOfflineList);
            }

            List<LocalImage> innerList = item.getRiskPoints();
            if (innerList != null) {
                for (LocalImage inner : innerList) {
                    inner.setParentId(item.getRecordId());

                    if (TextUtils.isEmpty(inner.getAttachLocal())) {
                        // 待上传的附件
                        inner.setAttachMode("5");
                        inner.setAttachStatus("01");
                    } else {
                        // 已上传的附件
                        inner.setAttachMode("3");
                        inner.setAttachStatus("02");
                    }
                }

                new LocalImageImpl().insertList(innerList);
            }
        }
    }

    /**
     *
     * 非离线风险点数据保存
     */
    public void insertOnlineList(List<TicketMeasureSave> list) {
        super.insertList(list);

        for (TicketMeasureSave item : list) {
            List<LocalImage> childList = new LocalImageImpl().findByParam(
                    new Property[]{
                            LocalImageDao.Properties.ParentId,
                            LocalImageDao.Properties.AttachMode
                    },
                    new String[]{item.getRecordId(), "3"}
            );

            if (childList != null) {
                new LocalImageImpl().delete(childList);
            }

            List<LocalImage> innerList = item.getRiskPoints();
            if (innerList != null) {
                for (LocalImage inner : innerList) {
                    inner.setParentId(item.getRecordId());
                    inner.setAttachMode("3");
                    inner.setAttachStatus("02");
                }

                new LocalImageImpl().insertList(innerList);
            }
        }
    }

    /**
     * 根据工单号、计划编号、工作票查询列表
     */
    public List<TicketMeasureSave> findRiskList(String AppNo, String planNo, String wtNo) {
        return findByParam(
                new Property[]{
                        TicketMeasureSaveDao.Properties.AppNo,
                        TicketMeasureSaveDao.Properties.PlanNo,
                        TicketMeasureSaveDao.Properties.WtNo
                },
                new String[]{AppNo, planNo, wtNo}
        );
    }

}
